package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户业务接口实现类
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Override
    public IPage<UserBean> findByItem(int pageNO, String name) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if(name != null && name.length() != 0){
            qw.like("u_name",name);
        }

        return userMapper.selectPage(
                new Page<UserBean>(pageNO,PAGESIZE),qw);
    }

    @Override
    public void add(UserBean user, int[] roleArray) {
        userMapper.insert(user);
        userMapper.addUserRole(user.getId(),roleArray);
    }

    @Override
    public void del(int userId) {
        userMapper.del(userId);
    }

    @Override
    public UserBean findById(int userId) {
        return userMapper.findById(userId);
    }

    @Override
    public void updateRole(int userId, int[] roleArray) {
        userMapper.updateRole(userId,roleArray);
    }
}
